package com.htfyun.fragment.postil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;

import com.htfyun.database.DaoSession;
import com.htfyun.database.GreenDaoManager;
import com.htfyun.database.PWNote;
import com.htfyun.database.PWNoteDao;
import com.htfyun.fragment.base.BaseWithPageNumberAdapter;
import com.htfyun.fragment.base.BaseWithPageNumberFragment;
import com.htfyun.utils.DebugLog;
import com.htfyun.view.CustomGridLayoutManager;
import com.htfyun.view.GridSpacingItemDecoration;

import org.geometerplus.zlibrary.ui.android.R;
import org.greenrobot.greendao.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import me.yokeyword.fragmentation.SupportFragment;

import static com.htfyun.Constants.ACTION_SAVE_BMP;

/**
 * Created by sst on 2017/12/28.
 */

public class HtfyunPWFragment extends BaseWithPageNumberFragment<PWNote> {

    public static final String PICASSO_TAG = "HtfyunPWFragment";

    private static final int SPAN_COUNT = 4;

    private Query<PWNote> notesQuery;
    private PWNoteDao noteDao;

    List<PWNote> pwNoteList = new ArrayList<>();
    
    private BaseWithPageNumberAdapter mAdapter;


    public static HtfyunPWFragment newInstance() {

        Bundle args = new Bundle();

        HtfyunPWFragment fragment = new HtfyunPWFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public HtfyunPWFragment() {
        // get the note DAO
        DaoSession daoSession = GreenDaoManager.INSTANCE.getDaoSession();
        noteDao = daoSession.getPWNoteDao();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onSupportVisible() {

        pwNoteList.clear();
        pwNoteList.addAll(noteDao.loadAll());

        super.onSupportVisible();

        DebugLog.e("HtfyunPWFragment onSupportVisible");
        
        
        
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new CustomGridLayoutManager(_mActivity, SPAN_COUNT);
    }

    @Override
    public RecyclerView.ItemDecoration getDecor() {
        int space = getContext().getResources().getDimensionPixelSize(R.dimen.adapter_pw_item_img_cover_space);
        return (new GridSpacingItemDecoration(SPAN_COUNT, space, true));
    }

    @Override
    public BaseWithPageNumberAdapter getAdapter() {

        if (mAdapter == null) {
            mAdapter = new HtfyunPWAdapter(_mActivity);
        }
        return mAdapter; 
    }

    @Override
    public int getItemCountPerPage() {
        return getResources().getInteger(R.integer.pw_count_per_page) - 1;
    }

    @Override
    public List<PWNote> getTotalItemList() {
       
        return pwNoteList;
    }

    @Override
    public String getStringForTextEmpty() {
        return "";
    }
    
//    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//
//            String action = intent.getAction();
//            if (action == null) {
//                return;
//            }
//            if (action.equals(ACTION_SAVE_BMP)) {

//                pwNoteList.clear();
//                pwNoteList.addAll(noteDao.loadAll());
//
//                getAdapter().notifyDataSetChanged();

                
//                mHandler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        getAdapter().notifyDataSetChanged();
//                    }
//                }, 1000);
//                
//                DebugLog.e("recv action save bmp");
//            
//            }
//        }
//    };

    private Handler mHandler = new Handler() ;
}
